package org.example.service;

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.common.configs.mail.AsyncMailHelper;
import org.example.common.support.FreemarkerSupport;
import org.example.common.utils.AESEncryptUtils;
import org.example.model.entity.Class;
import org.example.model.entity.SysConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author weigang
 * @create 2022-10-27 16:27
 */
@Service
@AllArgsConstructor
@Slf4j
public class testSendMailServiceImpl implements testSendMailService {
    private final FreemarkerSupport freemarkerSupport;
    private final MailAccount mailAccount;
    @Autowired
    private AsyncMailHelper asyncMailHelper;
    private final SysConfig sysConfig;


    @Override
    public void testSendCommitMail() {
        String str1 = "weigang";
        asyncMailHelper.send(() -> sendCommitMail(str1));
    }

    @Override
    public void testSendCheckMail(){
        String str1 = "weigang";
        asyncMailHelper.send(() -> sendCheckMail(str1));
    }

    public void sendCommitMail(String str1) throws IOException, freemarker.template.TemplateException {
        Set<String> emailStrs = Sets.newHashSet();
        String emailStrs1 = "isgangzi@163.com";
        String emailStrs2 = "weigang1@lenovo.com";
        emailStrs.add(emailStrs1);
        emailStrs.add(emailStrs2);

        try {
            MailUtil.send(
                    mailAccount,
                    String.join(StringPool.COMMA, emailStrs),
                    "Commit Email",
                    getCommitMailHtml(str1),
                    true);
        }catch (IOException | TemplateException e) {
            log.error("send commit email failed, usernames:{}", str1, e);
        }

    }

    public void sendCheckMail(String str1) throws IOException, TemplateException {
        Set<String> emailStrs = Sets.newHashSet();
        String emailStrs1 = "isgangzi@163.com";
        String emailStrs2 = "weigang1@lenovo.com";
        emailStrs.add(emailStrs1);
        emailStrs.add(emailStrs2);

        MailUtil.send(
                mailAccount,
                String.join(StringPool.COMMA, emailStrs),
                "Check Email",
                getCheckMailHtml(str1),
                true);
    }

    public String getCommitMailHtml(String str1) throws IOException, freemarker.template.TemplateException {
        Template template = freemarkerSupport.getTemplate("CommitMail.ftl");
        Class cl = new Class();
        cl.setClassId(1L);
        cl.setClassName(str1);
        List<Class> classList = Stream.of(cl).collect(Collectors.toList());

        Map<Object, Object> params = Maps.newHashMap();
        params.put("contents", classList);
        params.put("pageContentUrl", sysConfig.getPageContentUrl());

        return FreeMarkerTemplateUtils.processTemplateIntoString(template, params);
    }

    public String getCheckMailHtml(String str1) throws IOException, freemarker.template.TemplateException {
        Template template = freemarkerSupport.getTemplate("CheckMail.ftl");
        Class cl = new Class();
        cl.setClassId(1L);
        cl.setClassName(str1);
        List<Class> classList = Stream.of(cl).collect(Collectors.toList());


        String aesClassId = AESEncryptUtils.parseByte2HexStr(Objects.requireNonNull(AESEncryptUtils.encrypt(String.valueOf(cl.getClassId()), sysConfig.getAesKey())));
        String aesClassName = AESEncryptUtils.parseByte2HexStr(Objects.requireNonNull(AESEncryptUtils.encrypt(cl.getClassName(), sysConfig.getAesKey())));
        cl.setCheckSuccessUrl(sysConfig.getCheckPageContentUrl() + "success?cpi=" + aesClassId + "&ic=" + aesClassName);
        cl.setCheckFailUrl(sysConfig.getCheckPageContentUrl() + "fail?cpi=" + aesClassId + "&ic=" + aesClassName);


        Map<Object, Object> params = Maps.newHashMap();
        params.put("contents", classList);
        params.put("pageContentUrl", sysConfig.getPageContentUrl());

        return FreeMarkerTemplateUtils.processTemplateIntoString(template, params);
    }
}
